package com.graduation_design.modules.admin.controller;

import cn.hutool.core.map.MapUtil;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppOrder;
import com.graduation_design.modules.app.controller.BaseController;
import com.graduation_design.service.AppOrderService;
import com.graduation_design.utils.OrderStatusUtil;
import com.graduation_design.utils.ShiroUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@RestController
@RequestMapping("/admin/order")
public class AdminOrderController extends BaseController {

    @Resource
    AppOrderService appOrderService;

    @GetMapping("/list")
    public Result list(AppOrder appOrder) {
        return Result.success(MapUtil.builder()
                .put("pageData", appOrderService.pageAdmin(getPage(), appOrder))
                .put("orderStatusList", OrderStatusUtil.getStatusList())
                .build()
        );
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id) {
        return Result.success(appOrderService.getOrderInfo(id));
    }

    @PostMapping("/ship")
    public Result ship(@RequestBody AppOrder appOrder) {
        appOrderService.shipAdmin(appOrder);
        return Result.success();
    }

    @GetMapping("/deliveryInfo")
    public Result deliveryInfo(Long id) throws Exception {
        AppOrder appOrder = appOrderService.getById(id);
        return Result.success(appOrderService.getDeliveryInfo(appOrder));
    }

    @PostMapping("/close")
    public Result close(@RequestBody AppOrder appOrder) {
        Long adminId = ShiroUtil.getProfile().getId();
        appOrderService.closeAdmin(appOrder.getId(), appOrder.getNote(), adminId);
        return Result.success();
    }
}
